from com.arcfox.util.util import try_parse_int, multi_replace

font_mapper = {
    "&#xe622": "0", "&#xe639": "1",
    "&#xe637": "2", "&#xe636": "3",
    "&#xe62f": "4", "&#xe62e": "5",
    "&#xe62d": "6", "&#xe628": "7",
    "&#xe627": "8", "&#xe624": "9",
    # &#xe650对应的是没数据, 返回0
    "&#xe650": "0"
}


def decrypt_font(_s):
    '''
    聚志愿字体解密
    :param self:
    :param _s:
    :return:
    '''
    _s = '' + _s
    t = []
    st = 0
    while True:
        if st > len(_s):
            break
        l = _s[st:st + 1]
        l = try_parse_int(l)
        if not isinstance(l, int):
            break
        t.append(_s[st:st + l + 3])
        st += (l + 3)
    r = ''
    for i in t:
        _ss = i[1:]
        _ss = multi_replace(_ss, [0, 1, 5, 9], '')
        _ss = multi_replace(_ss, ["a", "b", "c", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s",
                                  "t", "u", "w"], '')
        ts = list(_ss)
        ts.sort()
        if len(ts) == 1:
            ts.append('9')
        if ts[1] == '3' and (ts[0] == '2' or ts[0] == '1'):
            p = ts[0]
            ts[0] = ts[1]
            ts[1] = p
        if ts[0] == ts[1] and ts[0] == '4':
            ts[0] = '5'
            ts[1] = '0'
        r += ('&#xe6' + "".join(ts) + ';')
    font = ""
    for c in filter(None, r.split(";")):
        font += font_mapper[c]
    return int(font)
